package com.collect.senegal.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiRealTime;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiLevelDetail.ZHILevelDetailGroupRealTime;
import com.collect.senegal.zhiBase.BaseActivity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRPerfermanceGroupRealTimeEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiSort.ZHIGroupRealTimeCompareByTotalNumASC;
import com.collect.senegal.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiSort.ZHIGroupRealTimeCompareByTotalNumDES;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_PERFORMANCE_GROUP_REAL_TIME)
public class ZHIPerformanceGroupRealTimeActivity extends BaseActivity<ZHIPerformanceGroupRealTimePresenter> implements ZHIPerformanceGroupRealTimeContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_total_num)
    TextView tv_total_num;
    @BindView(R.id.tv_add_num)
    TextView tv_add_num;
    @BindView(R.id.tv_settle_num)
    TextView tv_settle_num;
    @BindView(R.id.tv_not_mark_num)
    TextView tv_not_mark_num;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    private final ZHILevelDetailGroupRealTime mZHILevelDetailGroupRealTime = new ZHILevelDetailGroupRealTime();

    private ArrayList<ZHIRPerfermanceGroupRealTimeEntity> mZhirPerfermanceGroupRealTimeEntityArrayList;

    private boolean mIsArraySortDesNow = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_performance_group_real_time;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void initView() {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            iv_filter.setOnClickListener(v -> finish());

            iv_sort.setOnClickListener(v -> {
                if (mZhirPerfermanceGroupRealTimeEntityArrayList != null) {
                    if (mIsArraySortDesNow) {
                        Collections.sort(mZhirPerfermanceGroupRealTimeEntityArrayList, new ZHIGroupRealTimeCompareByTotalNumASC());
                        mZHILevelDetailGroupRealTime.notifyDataSetChanged();
                        mIsArraySortDesNow = false;
                    } else {
                        Collections.sort(mZhirPerfermanceGroupRealTimeEntityArrayList, new ZHIGroupRealTimeCompareByTotalNumDES());
                        mZHILevelDetailGroupRealTime.notifyDataSetChanged();
                        mIsArraySortDesNow = true;
                    }
                }
            });

            mZHILevelDetailGroupRealTime.setEmptyView(getRefreshEmptyView());

            rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_detail_list.setAdapter(mZHILevelDetailGroupRealTime);

            if (mPresenter != null) {
                mPresenter.getGroupRealTimeList(ZHIPerformanceGroupRealTimeActivity.this, new ZHISPerfermanceEntity(), true);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetGroupRealTimeList(ArrayList<ZHIRPerfermanceGroupRealTimeEntity> zhirPerfermanceGroupRealTimeEntityArrayList, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            int gtotal = 0;
            int gadd = 0;
            int gsettle = 0;
            int gnotmark = 0;

            if (mZhirPerfermanceGroupRealTimeEntityArrayList == null) {
                mZhirPerfermanceGroupRealTimeEntityArrayList = new ArrayList<>();
            }
            mZhirPerfermanceGroupRealTimeEntityArrayList.clear();

            for (int i = 0; i < zhirPerfermanceGroupRealTimeEntityArrayList.size(); i++) {
                ZHIRPerfermanceGroupRealTimeEntity zhirPerfermanceGroupRealTimeEntity = zhirPerfermanceGroupRealTimeEntityArrayList.get(i);
                gtotal += zhirPerfermanceGroupRealTimeEntity.getTotalNum();
                gadd += zhirPerfermanceGroupRealTimeEntity.getTodayAddNum();
                gsettle += zhirPerfermanceGroupRealTimeEntity.getTodaySettleNum();
                gnotmark += zhirPerfermanceGroupRealTimeEntity.getTodayNotMarkNum();

                mZhirPerfermanceGroupRealTimeEntityArrayList.add(zhirPerfermanceGroupRealTimeEntity);
            }

            tv_total_num.setText(gtotal + "");
            tv_add_num.setText(gadd + "");
            tv_settle_num.setText(gsettle + "");
            tv_not_mark_num.setText(gnotmark + "");

            Collections.sort(mZhirPerfermanceGroupRealTimeEntityArrayList, new ZHIGroupRealTimeCompareByTotalNumASC());

            mZHILevelDetailGroupRealTime.setNewData(mZhirPerfermanceGroupRealTimeEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initData(boolean showDialog) {
        if (mPresenter != null) {
            mPresenter.getGroupRealTimeList(ZHIPerformanceGroupRealTimeActivity.this, new ZHISPerfermanceEntity(), showDialog);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }
}